<?php declare(strict_types=1);
/**
 * @author      guosir
 * @copyright   2023-2025 owner
 **/

namespace Srv\Apps\Apis\Task;

use Srv\Apps\Apis\Log\Ctl\LogCtl;
use Srv\Apps\Apis\Task\Ctl\LotteryCtl;
use Srv\Apps\Proto\DTO\Task\TaskLotteryItemListDTO;
use Srv\Apps\Proto\DTO\Task\TaskLotteryItemAddDTO;
use Srv\Apps\Proto\DTO\Task\TaskLotteryItemEditDTO;
use Srv\Apps\Proto\Enum\ErrCodeEnum;
use Srv\Apps\Proto\Enum\LangKeyEnum;
use Srv\Apps\Proto\Enum\Log\LogResultEnum;
use Srv\Apps\Proto\VO\Task\TaskLotteryItemListVO;
use Srv\Libs\ConfModel\RouteConfData;
use Srv\Libs\Frame\ActionAbstract;
use Srv\Libs\Frame\DTOAbstract;
use Srv\Libs\Frame\Lang;
use Srv\Libs\Frame\Page;
use Srv\Libs\Frame\ResponseData;
use Srv\Libs\Frame\VOAbstract;

final class LotteryAct extends ActionAbstract
{
    /**
     * @url /lottery/item/list
     * @auth getLotteryItemList
     * @req Srv.Apps.Proto.DTO.Task.TaskLotteryItemListDTO
     * @rsp Srv.Apps.Proto.VO.Task.TaskLotteryItemListVO
     * 转盘奖项列表
     */
    public function getLotteryItemList(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        if(!($DTOClass instanceof TaskLotteryItemListDTO) || !($VOClass instanceof TaskLotteryItemListVO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $Page                   = new Page();
        $currPage               = max(1, $DTOClass->getCurrPage());
        $pageSize               = $DTOClass->getPageSize() < 1 ? 12 : $DTOClass->getPageSize();
        $Page->init(0, $pageSize, $currPage);
        $whereData              = $RouteConfData->getParamsResData($DTOClass);
        $VOClass                = LotteryCtl::tag()->getLotteryItemList($Page, $whereData);
        return ResponseData::rspSuccAuto('', $VOClass, []);
    }

    /**
     * @url /lottery/item/add
     * @auth lotteryItemAdd
     * @req Srv.Apps.Proto.DTO.Task.TaskLotteryItemAddDTO
     * 转盘奖项新增
     */
    public function lotteryItemAdd(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof TaskLotteryItemAddDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $logData        = $setData;
        $LogCtl         = LogCtl::tag();
        $tlyiId         = LotteryCtl::tag()->lotteryItemAdd($setData);
        if($tlyiId < 1){
            $errMsg     = '转盘奖项新增失败';
            $LogCtl->logAdd(LogCtl::LOTTERY_ITEM_ADD, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '转盘奖项新增成功';
        $LogCtl->logAdd(LogCtl::LOTTERY_ITEM_ADD, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }

    /**
     * @url /lottery/item/edit
     * @auth lotteryItemEdit
     * @req Srv.Apps.Proto.DTO.Task.TaskLotteryItemEditDTO
     * 转盘奖项编辑
     */
    public function lotteryItemEdit(RouteConfData $RouteConfData, ?DTOAbstract $DTOClass, ?VOAbstract $VOClass):bool
    {
        unset($VOClass);
        if(!($DTOClass instanceof TaskLotteryItemEditDTO)) return ResponseData::rspErrAuto(ErrCodeEnum::ERR_SYS_PARAMS_ERROR, Lang::get(LangKeyEnum::LANG_SYS_PARAMS_ERROR));
        $tlyiId         = intval($DTOClass->getTlyiId());
        $setData        = $RouteConfData->getParamsResData($DTOClass);
        $LotteryCtl     = LotteryCtl::tag();
        $LogCtl         = LogCtl::tag();
        $infoSrc        = $LotteryCtl->getLotteryItemSrc($tlyiId);
        if(!isset($infoSrc['tlyi_id'])){
            $logData    = $setData;
            $errMsg     = '转盘奖项不存在';
            $LogCtl->logAdd(LogCtl::LOTTERY_ITEM_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $logData        = ['new' => $setData, 'old' => $infoSrc];
        $result         = $LotteryCtl->lotteryItemEdit($tlyiId, $setData);
        if(!$result){
            $errMsg     = '转盘奖项编辑失败';
            $LogCtl->logAdd(LogCtl::LOTTERY_ITEM_EDIT, LogResultEnum::LOG_RESULT_FAIL, $errMsg, $logData);
            return ResponseData::rspErrAuto(ErrCodeEnum::ERR_FAIL, Lang::getString(LangKeyEnum::_LANG_FAILED));
        }
        $errMsg         = '转盘奖项编辑成功';
        $LogCtl->logAdd(LogCtl::LOTTERY_ITEM_EDIT, LogResultEnum::LOG_RESULT_SUCCESS, $errMsg, $logData);
        return ResponseData::rspSuccAuto('', null, []);
    }
}